#include "StdAfx.h"
#include "SysConfig.h"
#include <io.h>
#include <direct.h>
#include "FilyFunction.h"

std::wstring CSysConfig::m_ConfigFileName = L"ConfigFile.ini";

CSysConfig::CSysConfig(void)
{
}


CSysConfig::~CSysConfig(void)
{
}

// SetConfigFileName
void CSysConfig::SetConfigFileName(const std::wstring& filename)
{
	int inum = 0;
	m_ConfigFileName = filename;
	
	while((inum   =   m_ConfigFileName.find(_T("\\\\")))>0)   
		m_ConfigFileName.replace(inum,2,_T("\\"));
}


// ReadConfigData
bool CSysConfig::ReadConfigData(const std::wstring& type,
	const std::wstring& param, std::wstring& value)
{
	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	GetPrivateProfileString(type.c_str(),param.c_str(), _T(""), data, MAX_PATH, m_ConfigFileName.c_str());
	if(wcslen(data) <1)
	{
		value = L"";
		return false;
	}
	value = data;	
	return true;
}


bool CSysConfig::ReadConfigData(const std::wstring& type,
	const std::wstring& param, std::string& value)
{
	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	GetPrivateProfileString(type.c_str(),param.c_str(), _T(""), data, MAX_PATH, m_ConfigFileName.c_str());
	if(wcslen(data) <1)
	{
		value = "";
		return false;
	}
	char tmpdata[MAX_PATH];	
	memset(tmpdata,0,sizeof(tmpdata));
	CFilyFunction::WCharToMByte(data,tmpdata,255);
	value = tmpdata;	
	return true;
}


bool CSysConfig::ReadConfigData(const std::wstring& type,
	const std::wstring& param, int & value)
{
	value = GetPrivateProfileInt(type.c_str(),param.c_str(), -1, m_ConfigFileName.c_str());
	if(value == -1)
	{
		return false;
	}	
	return true;
}


bool CSysConfig::ReadConfigData(const std::wstring& type,
	const std::wstring& param, float& value)
{
	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	GetPrivateProfileString(type.c_str(),param.c_str(), _T(""), data, MAX_PATH, m_ConfigFileName.c_str());
	if(wcslen(data) <1)
	{
		value = 0.0;
		return false;
	}
	value = _wtof(data);	
	return true;
}


bool CSysConfig::ReadConfigData(const std::wstring& type,
	const std::wstring& param, CString& value)
{

	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	GetPrivateProfileString(type.c_str(),param.c_str(), _T(""), data, MAX_PATH, m_ConfigFileName.c_str());
	if(wcslen(data) <1)
	{
		value = L"";
		return false;
	}
	value = data;	
	return true;
}


// WriteConfigData
bool CSysConfig::WriteConfigData(const std::wstring& type,
	const std::wstring& param, std::wstring& value)
{
	bool bwrite = WritePrivateProfileString(type.c_str(),param.c_str(),  value.c_str(), m_ConfigFileName.c_str());
	return bwrite;
}


bool CSysConfig::WriteConfigData(const std::wstring& type,
	const std::wstring& param, std::string& value)
{
	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	CFilyFunction::MByteToWChar(value.c_str(),data,MAX_PATH);
	std::wstring strdata = data;
	bool bwrite = WriteConfigData(type,param,strdata);
	return true;
}


bool CSysConfig::WriteConfigData(const std::wstring& type,
	const std::wstring& param, int & value)
{
	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	wsprintf(data,_T("%d"),value);
	std::wstring strdata = data;
	bool bwrite = WriteConfigData(type,param,strdata);
	return true;
}


bool CSysConfig::WriteConfigData(const std::wstring& type,
	const std::wstring& param, float& value)
{
	TCHAR   data[MAX_PATH];	
	memset(data,0,sizeof(data));
	wsprintf(data,_T("%f"),value);
	std::wstring strdata = data;
	bool bwrite = WriteConfigData(type,param,strdata);
	return true;
}


bool CSysConfig::WriteConfigData(const std::wstring& type,
	const std::wstring& param, const CString& value)
{
	bool bwrite = WritePrivateProfileString(type.c_str(),param.c_str(),  value, m_ConfigFileName.c_str());
	return bwrite;
}




//CreateSavePath
bool CSysConfig::CreateSavePath(const std::string& path)
{
	const char *fileName=path.c_str(),*tag;
	for(tag=fileName;*tag;tag++)
	{
		if (*tag=='\\')
		{
			char buf[1000],path[1000];
			strcpy(buf,fileName);
			buf[strlen(fileName)-strlen(tag)+1]=NULL;
			strcpy(path,buf);
			if (access(path,6)==-1)
			{
				mkdir(path);
			}
		}
	}
	return true;
}